<?php
namespace app\providers\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

// 命令  php think views:link

class ViewsLinkCommand extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('views:link');
        // 设置参数
        $this->setDescription('Create a symbolic link from "public/admins/views" to "views"');
    }

    protected function execute(Input $input, Output $output)
    {
    	// 指令输出
        // halt(public_path('admins').'views');

        $this->link( root_path('views') , public_path('admins').'views' );
        // $this->link( root_path('runtime').'storage/uploads' , public_path().'uploads' );
        // 指令输出
        $output->writeln('成功!');
    }
    
    
    public function link($target, $link)
    {
        if ( strtolower(substr(PHP_OS, 0, 3)) === 'win' ) {
         	$mode = $this->isDirectory($target) ? 'J' : 'H';
       	 	exec("mklink /{$mode} ".escapeshellarg($link).' '.escapeshellarg($target));
        }else{
        	return symlink($target, $link);
        }
    }
    
    public function isDirectory($directory)
    {
        return is_dir($directory);
    }

}



